package o2o.web.actions.postman;

import com.jinyou.utils.common.*;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import o2o.cache.agent.PostmanAgentRelationMemory;
import o2o.cache.comment.OrderCommentMemory;
import o2o.cache.company.postman.PostmanSignCompanyMemory;
import o2o.cache.order.OrderInfoMemory;
import o2o.cache.postman.CityPostmanMemory;
import o2o.cache.postman.PostmanTroubleOptionMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.cache.wallet.UserWalletMemory;
import o2o.config.dao.UserDaoSql;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.company.postman.PostCompanySalaryInfo;
import o2o.dao.PO.company.postman.PostmanSignCompanyInfo;
import o2o.dao.PO.company.postman.SIGN_COMPANY_STATUS;
import o2o.dao.PO.order.COMMENT_APPLY;
import o2o.dao.PO.order.ORDER_STATUS;
import o2o.dao.PO.order.OrderCommentInfo;
import o2o.dao.PO.postman.*;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.PO.wallet.UserWallet;
import o2o.dao.VO.common.FileUploadInfo;
import o2o.dao.VO.order.OrderCommentInfoVO;
import o2o.postman.model.CityPostman;
import o2o.service.company.postman.PostManSalaryService;
import o2o.system.data.PROMPTS;
import o2o.system.data.SYS_CONFIG_PATH;
import o2o.system.data.SYS_SETTINGS;
import o2o.utils.MultilingualResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

//配送员管理（骑手端用）
@Controller
public class PostmanAction {


    // 骑手排行
    @RequestMapping(method = RequestMethod.POST, value = "/postman/ranking")
    @ResponseBody
    public String postman_statistics_ranking(String token, String city, String province, String county, Long startTime, Long endTime
            , Integer page, Integer size) {

        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 20;
        }
        StringBuffer sql = new StringBuffer();
        sql.append("  SELECT  ")
                .append("      IFNULL(UI.NAME,'') AS name,")
                .append("       UI.`signPhoto` AS photo,")
                .append("       CP.`username` AS username,")
                .append("       IFNULL(II.totalMoney,0.0) AS totalMoney,")
                .append("       IFNULL(II.orderCounts,0) AS orderCounts")
                .append(" FROM      ").append(PojoTables.CityPostman).append(" as CP  ")
                .append("  LEFT JOIN      ").append(PojoTables.UserInfo).append(" AS UI ")
                .append("       ON CP.username = UI.username    ")
                .append("  LEFT JOIN     ")
                .append("     (SELECT  ")
                .append("               PO.username AS username,  ")
                .append("               SUM(IFNULL(PO.totalMoney, 0.0)) AS totalMoney,  ")
                .append("               SUM(IFNULL(PO.orderCounts, 0)) AS orderCounts  ")
                .append("        FROM   ").append(PojoTables.POSTMAN_ORDER_DAILY_STATISTICS).append(" AS PO ")
                .append("           WHERE PO.delFlag = 0");

        if (ValidateUtil.isAbsLong(startTime)) {
            sql.append("               AND PO.localdayTime >=  ").append(startTime);
        }
        if (ValidateUtil.isAbsLong(endTime)) {
            sql.append("               AND PO.localdayTime <  ").append(endTime);
        }
        sql.append(" GROUP BY PO.username)  AS II ON CP.username = II.username ");
        sql.append(" where CP.delFlag = 0 ")
                .append(" AND UI.delFlag =0 ");

        // 省
        if (ValidateUtil.isNotNull(province)) {
            sql.append("  AND CP.province like '%").append(province).append("%'");
        }
        // 市
        if (ValidateUtil.isNotNull(city)) {
            sql.append("  AND CP.city like '%").append(city).append("%'");
        }
        // 区
        if (ValidateUtil.isNotNull(county)) {
            sql.append("  AND CP.county like '%").append(county).append("%'");
        }

        sql.append(" ORDER BY orderCounts DESC ");


        List<HashMap<String, Object>> list = null;
        try {
            list = UserDaoSql.getInstance().queryBySql(sql.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        // 通过token获取骑手商圈
        List<String> tmpList = null;
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user != null) {
            // 获取代理商圈
            Long agentId = PostmanAgentRelationMemory.getInstance().getAgentByPostmanUsername(user.getUsername());
            // 通过商圈获取所有骑手
            if (agentId != null) {
                tmpList = PostmanAgentRelationMemory.getInstance().listOfAgentId(agentId);
            }
        }
        // 遍历所有骑手 跟商圈下的骑手对比
        List<HashMap<String, Object>> agentList = new ArrayList<>();
        for (HashMap<String, Object> stringObjectHashMap : list) {
            if (tmpList != null && tmpList.size() > 0) {
                if (tmpList.contains(stringObjectHashMap.get("username"))) {
                    agentList.add(stringObjectHashMap);
                }
            } else {
                agentList.add(stringObjectHashMap);
            }
        }

        if (ValidateUtil.isAbsList(agentList)) {
            JYPageUtils.PageList pageList = JYPageUtils.getList(agentList, page, size);
            List<HashMap<String, Object>> resultList = (List<HashMap<String, Object>>) pageList.list;
            if (ValidateUtil.isAbsList(resultList)) {
                for (HashMap<String, Object> map : resultList) {
                    if (null == map) {
                        continue;
                    }
                    Object obj = map.get("photo");
                    if (!ValidateUtil.isNull(obj) && ValidateUtil.isNotNetUrl(obj.toString())) {
                        String path = SYS_CONFIG_PATH.IMAGE_BASE_URL + obj.toString();
                        map.put("photo", path);
                    }
                }
            }

            return AjaxResult.successResult(pageList.totalCount, pageList.size, resultList);
        }
        //SELECT
//        CP.username AS username,
//                UI.NAME AS NAME,
//        IFNULL(II.totalMoney,0.0) AS totalMoney,
//        IFNULL(II.orderCounts,0) AS orderCounts
//        FROM
//        city_postman AS CP
//        LEFT JOIN user_info AS UI
//        ON CP.username = UI.username
//        LEFT JOIN
//        (SELECT
//        PO.username AS username,
//                SUM(IFNULL(PO.totalMoney, 0.0)) AS totalMoney,
//        SUM(IFNULL(PO.orderCounts, 0)) AS orderCounts
//        FROM
//        postman_order_daily_statistics AS PO
//        WHERE PO.delFlag = 0
//        AND PO.localdayTime >= 0
//        AND PO.localdayTime < 1559245600493
//        GROUP BY PO.username)  AS II ON CP.username = II.username
//        WHERE CP.delFlag = 0
//        AND UI.delFlag =0
//        AND CP.city ='济南';
        return AjaxResult.successResult(0, 0, null);
    }

    //获取配送员的营业信息
    @RequestMapping(method = RequestMethod.POST, value = "/postman/statistics")
    @ResponseBody
    public String postman_statistics(String token) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(user.getUsername());
        if (null == cityPostman) {
            return AjaxResult.errorResult("未查询到配送员信息");
        }
        PostmanStatistics postmanStatistics = new PostmanStatistics();
        if (SYS_SETTINGS.CUSTOM_SYSTEM_TYPE != null && SYS_SETTINGS.CUSTOM_SYSTEM_TYPE == 1) {
            List<PostmanSignCompanyInfo> companyInfos = PostmanSignCompanyMemory.getInstance().listOfPostman(user.getUsername(), null, null, SIGN_COMPANY_STATUS.pass);
            if (ValidateUtil.isAbsList(companyInfos)) {
                Set<Long> companyIdSet = companyInfos.stream().map(b -> b.getCompanyId()).collect(Collectors.toSet());
                double money = 0.0d;
//                for (PostmanSignCompanyInfo info : companyInfos) {
//                    if (ValidateUtil.isID(info.getCompanyId())) {
//                        try {
//                            PostCompanySalaryInfo salary = PostManSalaryService.getInstance().getSalary(user.getUsername(), info.getCompanyId());
//                            if (salary != null) {
//                                money = JYMathDoubleUtils.add(money, JYMathDoubleUtils.add(salary.getMoney(), salary.getAllowance()));
//                                if (ValidateUtil.isAbsDouble(salary.getApplyMoney())) {
//                                    money = JYMathDoubleUtils.sub(money, salary.getApplyMoney());
//                                }
//                            }
//                        } catch (SQLException e) {
//                            e.printStackTrace();
//                            continue;
//                        }
//                    }
//                }
                for (Long companyIdT : companyIdSet) {
                    if (ValidateUtil.isID(companyIdT)) {
                        try {
                            PostCompanySalaryInfo salary = PostManSalaryService.getInstance().getSalary(user.getUsername(), companyIdT);
                            if (salary != null) {
                                money = JYMathDoubleUtils.add(money, JYMathDoubleUtils.add(salary.getMoney(), salary.getAllowance()));
                                if (ValidateUtil.isAbsDouble(salary.getApplyMoney())) {
                                    money = JYMathDoubleUtils.sub(money, salary.getApplyMoney());
                                }
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                            continue;
                        }
                    }
                }
                postmanStatistics.setAccount(money);
                postmanStatistics.setFcAccount(money);
            } else {
                postmanStatistics.setAccount(0d);
                postmanStatistics.setTodayMoney(0d);
                postmanStatistics.setTodayOrderCount(0l);
            }
        } else {
            UserWallet userWallet = UserWalletMemory.getInstance().getWallet(user.getUsername());
            if (null != userWallet) {
                postmanStatistics.setAccount(userWallet.getMoney());
                postmanStatistics.setFcAccount(userWallet.getFcMoney());
            } else {
                postmanStatistics.setAccount(0d);
                postmanStatistics.setTodayMoney(0d);
                postmanStatistics.setTodayOrderCount(0l);
            }
        }
        DateUtils.StartEndOfDayTimestamp se = DateUtils.getStartEndTimestamp(DateUtils.getCurrTime());
        if (null != se) {
            Long startTime = se.startTimestamp;
            Long endTime = se.endTimestamp;
            //当日订单量
            Long count = OrderInfoMemory.getInstance().getPostmanOneDaysCount(user.getUsername(), startTime, endTime);
            postmanStatistics.setTodayOrderCount(count);

            if (null != SYS_SETTINGS.POSTMAN_SHARE_MONEY && 0 != SYS_SETTINGS.POSTMAN_SHARE_MONEY) {
//                //查询当日收入
//                StringBuffer sb = new StringBuffer();
//                sb.append("select sum(postmanMoney) postmanMoney from ").append(PojoTables.OrderPay).append(" where createTime>").append(startTime).append(" and createTime< ")
//                        .append(endTime).append(" and postman = '").append(user.getUsername()).append("' and isOrderCancel = 0");
//                HashMap<String, Object> dbMap = null;
//                try {
//                    dbMap = UserDaoSql.getInstance().queryMapBySql(sb.toString());
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }

                //查询当日收入
                StringBuffer sb = new StringBuffer();
                sb.append("SELECT SUM(a.postmanMoney) postmanMoney FROM order_pay a LEFT JOIN order_info b ON a.orderNo = b.orderNo WHERE a.createTime> ").append(startTime)
                        .append(" AND a.createTime< ").append(endTime).append(" AND a.postman = '").append(user.getUsername())
                        .append("' AND a.isOrderCancel = 0 AND a.delFlag = 0 AND b.delFlag = 0 ")
                        .append(" and ( b.orderStatus = ").append(ORDER_STATUS.OVER).append(" or b.orderStatus = ")
                        .append(ORDER_STATUS.POSTMAN_FINISH).append(" )");

                HashMap<String, Object> dbMap = null;
                try {
                    dbMap = UserDaoSql.getInstance().queryMapBySql(sb.toString());
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                if (null != dbMap && null != dbMap.get("postmanMoney")) {
                    Double money = Double.parseDouble(dbMap.get("postmanMoney") + "");
                    postmanStatistics.setTodayMoney(money);
                }
            }
        }

        //每月提现次数
        if (null == SYS_SETTINGS.SHOP_WALLET_POP_MONTH) {
            postmanStatistics.setMonthPopTimes(0);
        } else {
            postmanStatistics.setMonthPopTimes(SYS_SETTINGS.SHOP_WALLET_POP_MONTH);
        }

        postmanStatistics.setIsWork(cityPostman.getIsWork());
        postmanStatistics.setIsRemind(cityPostman.getIsRemind());
        postmanStatistics.setRemindTime(cityPostman.getRemindTime());

        return AjaxResult.successResult(postmanStatistics);
    }

    // 配送员订单统计
    @RequestMapping(method = RequestMethod.POST, value = "/postman/history/statistics")
    @ResponseBody
    public String getHistoryStatistics(String token, Long startTime, Long endTime, Integer page, Integer size) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(user.getUsername());
        if (null == cityPostman) {
            return AjaxResult.errorResult("未查询到配送员信息");
        }
        if (ValidateUtil.isNotAbsInteger(page)) {
            page = 1;
        }
        if (ValidateUtil.isNotAbsInteger(size)) {
            size = 20;
        }

        List<PostmanStatistics> postmanStatisticses = new ArrayList<>();
//        if (null != SYS_SETTINGS.POSTMAN_SHARE_MONEY && 0 != SYS_SETTINGS.POSTMAN_SHARE_MONEY) {
        //查询当日收入
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT FROM_UNIXTIME(payinfo.tim/1000,'%Y-%m-%d') AS tempTime ")
                .append(",SUM(IFNULL(payinfo.money,0)) as totalMoney")
                .append(",COUNT(1) as totalCount")
                .append("       FROM")
                .append("           (SELECT info.`postmanFinishTime` AS tim, pay.`postmanMoney` AS money FROM ").append(PojoTables.OrderInfo).append(" AS info")
                .append("                   LEFT JOIN ").append(PojoTables.OrderPay).append(" AS pay")
                .append("                           ON info.orderNo = pay.`orderNo` AND pay.`delFlag` = 0 AND pay.`isOrderCancel` = 0 AND pay.`isPaid` = 1 ")
                .append("                           WHERE info.delFlag = 0 AND info.postmanFinishTime  IS NOT NULL  ");
        if (SYS_SETTINGS.POSTING_CANCEL_STATISTICS_FINISH != null && SYS_SETTINGS.POSTING_CANCEL_STATISTICS_FINISH == 1) {
            sb
                    .append("                           And (( info.`orderStatus` =").append(ORDER_STATUS.OVER).append(" ")
                    .append("                                 or  info.`orderStatus` =").append(ORDER_STATUS.POSTMAN_FINISH).append(" )")
                    .append(" or (").append("( info.orderStatus =").append(ORDER_STATUS.CANCEL).append(" or info.orderStatus =").append(ORDER_STATUS.CANCEL_BY_SYSTEM)
                    .append(" or info.orderStatus =").append(ORDER_STATUS.PAYED_CANCEL).append(") and info.isPostingCancel  = 1")
                    .append(" )")
                    .append(" ) ");
        } else {
            sb
                    .append("                           And ( info.`orderStatus` =").append(ORDER_STATUS.OVER).append(" ")
                    .append("                                 or  info.`orderStatus` =").append(ORDER_STATUS.POSTMAN_FINISH).append(" )");
        }

        sb.append("                               AND info.orderNo not like 'S%' ")
                .append("                               AND info.postmanUsername = '").append(user.getUsername()).append("' ")
                .append("                               AND info.`postmanFinishTime`>").append(startTime)
                .append("                               AND INFO.`postmanFinishTime`<").append(endTime)
                .append("           ) AS payinfo GROUP BY tempTime ORDER BY tempTime DESC  ");
//                    .append(" LIMIT ").append((page-1)*size).append(",").append(size);
        List<HashMap<String, Object>> list = null;
        try {
            list = UserDaoSql.getInstance().queryBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (ValidateUtil.isAbsList(list)) {
            for (Map<String, Object> map : list) {
                if (map == null) {
                    continue;
                }
                PostmanStatistics postmanStatistics = new PostmanStatistics();
                if (null != map.get("totalMoney")) {
                    Double money = Double.parseDouble(map.get("totalMoney") + "");
                    postmanStatistics.setTodayMoney(money);
                }
                if (null != map.get("tempTime")) {
                    String temp = map.get("tempTime") + "";
                    postmanStatistics.setTempTime(temp);
                }
                if (null != map.get("totalCount")) {
                    Long count = Long.parseLong(map.get("totalCount") + "");
                    postmanStatistics.setTodayOrderCount(count);
                }
                postmanStatisticses.add(postmanStatistics);
            }
        }
//        }

        // 总量
        PostmanOrderTotalStatistics postmanOrderTotalStatistics = new PostmanOrderTotalStatistics();
        postmanOrderTotalStatistics.setStarTime(startTime);
        postmanOrderTotalStatistics.setEndTime(endTime);
        if (postmanStatisticses != null && postmanStatisticses.size() > 0) {
            Long count = 0L;
            Double money = 0.0;
            for (PostmanStatistics postmanStatistics : postmanStatisticses) {
                if (postmanStatistics.getTodayMoney() != null && postmanStatistics.getTodayMoney() > 0) {
                    money += postmanStatistics.getTodayMoney();
                } else {
                    money += 0;
                }
                if (postmanStatistics.getTodayOrderCount() != null && postmanStatistics.getTodayOrderCount() > 0) {
                    count += postmanStatistics.getTodayOrderCount();
                } else {
                    count += 0;
                }
            }
            postmanOrderTotalStatistics.setTotalOrderCount(count);
            postmanOrderTotalStatistics.setTotalMoney(money);
            if ((page - 1) * size < postmanStatisticses.size()) {
                if (page * size <= postmanStatisticses.size()) {
                    postmanOrderTotalStatistics.setPostmanStatisticses(postmanStatisticses.subList((page - 1) * size, page * size));
                } else {
                    postmanOrderTotalStatistics.setPostmanStatisticses(postmanStatisticses.subList((page - 1) * size, postmanStatisticses.size()));
                }
            }
        } else {
            postmanOrderTotalStatistics.setTotalOrderCount(0L);
            postmanOrderTotalStatistics.setTotalMoney(0.0);
        }
        return AjaxResult.successResult(postmanOrderTotalStatistics);
    }


    //配送员营业状态修改
    @RequestMapping(method = RequestMethod.POST, value = "/postman/status/modify")
    @ResponseBody
    public String postman_status_modify(String token, Integer isWork) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(user.getUsername());
        if (null == cityPostman) {
            return AjaxResult.errorResult("未查询到配送员信息");
        }

        if (null != SYS_SETTINGS.POSTMAN_MUST_VERIFY && 0 == 1 - SYS_SETTINGS.POSTMAN_MUST_VERIFY
                && (null == cityPostman.getIsVerify() || 0 != 1 - cityPostman.getIsVerify())) {
            return AjaxResult.errorResult("您尚未通过认证，请先去认证");
        }
        if (null != SYS_SETTINGS.POSTMAN_MUST_VERIFY_FACE && 0 == 1 - SYS_SETTINGS.POSTMAN_MUST_VERIFY_FACE) {
            if (null != user.getIsCert() && 0 == 1 - user.getIsCert()) {
                //已认证
            } else {
                return AjaxResult.errorResult("未通过实名认证，请先去认证");
            }
        }

        if (null == isWork) {
            return AjaxResult.errorResult("是否上班不能为空");
        }
        if (isWork != POSTMAN_WORK_STATUS.POSTMAN_WORK
                && isWork != POSTMAN_WORK_STATUS.POSTMAN_OFF_WORK
                && isWork != POSTMAN_WORK_STATUS.POSTMAN_BUSY) {
            //
            return AjaxResult.errorResult("请选择状态");
        }
//        StringBuilder sb = new StringBuilder();
//        sb.append("update ").append(PojoTables.CityPostman).append(" set isWork = ").append(isWork).append(",updateUser = '")
//                .append(user.getUsername()).append("' where username = '").append(user.getUsername()).append("' and delFlag = 0");
//        try {
//            UserDaoSql.getInstance().executeBySql(sb.toString());
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return AjaxResult.errorResult("修改失败，请稍后再试");
//        }
//        CityPostmanMemory.getInstance().setIsWork(user.getUsername(), isWork);
        ResultInfo resultInfo = PostmanMan.cityPostmanStatusModify(user.getUsername(), isWork);
        if (null != resultInfo) {
            if (!resultInfo.isSuccess()) {
                return AjaxResult.errorResult(resultInfo.getError());
            }
        } else {
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }

        return AjaxResult.successResult();
    }


    //工作设置
    @RequestMapping(method = RequestMethod.POST, value = "/postman/settings")
    @ResponseBody
    public String postman_settings(String token, Integer isWork, Integer isRemind, Integer remindTime) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(user.getUsername());
        if (null == cityPostman) {
            return AjaxResult.errorResult("未查询到配送员信息");
        }
        if (null == isWork) {
            isWork = cityPostman.getIsWork();
        }
        if (null == isRemind) {
            isRemind = cityPostman.getIsRemind();
        }
        if (null == remindTime) {
            remindTime = cityPostman.getRemindTime();
        }

        if (0 == 1 - isWork) {
            if (null != SYS_SETTINGS.POSTMAN_MUST_VERIFY && 0 == 1 - SYS_SETTINGS.POSTMAN_MUST_VERIFY
                    && (null == cityPostman.getIsVerify() || 0 != 1 - cityPostman.getIsVerify())) {
                return AjaxResult.errorResult("您尚未通过认证，请先去认证");
            }
            if (null != SYS_SETTINGS.POSTMAN_MUST_VERIFY_FACE && 0 == 1 - SYS_SETTINGS.POSTMAN_MUST_VERIFY_FACE) {
                if (null != user.getIsCert() && 0 == 1 - user.getIsCert()) {
                    //已认证
                } else {
                    return AjaxResult.errorResult("未通过实名认证，请先去认证");
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.CityPostman).append(" set isWork = ").append(isWork).append(",updateUser = '")
                .append(user.getUsername()).append("',isRemind=").append(isRemind).append(",remindTime=").append(remindTime).append(" where username = '").append(user.getUsername()).append("' and delFlag = 0");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }
        CityPostmanMemory.getInstance().setIsWork(user.getUsername(), isWork);
        CityPostmanMemory.getInstance().setRemind(user.getUsername(), isRemind, remindTime);
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.POSTMAN_WORK_SET).setStr01(user.getUsername()).setInt01(isWork);
            JmsSender.setTopicSender(notify);
            JMSNotify notify2 = JMSNotify.getInstance().setType(JMSTypeContent.POSTMAN_REMIND_SET).setStr01(user.getUsername()).setInt01(isRemind).setInt02(remindTime);
            JmsSender.setTopicSender(notify2);
        } catch (Exception e) {
        }
        return AjaxResult.successResult();
    }


    //获取配送订单异常常用语
    @RequestMapping(method = RequestMethod.POST, value = "/postman/trouble/option/list")
    @ResponseBody
    public String postman_trouble_options(String token) {
        //验证用户有效性
//        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
//        if (user == null) {
//            return AjaxResult.errorLogoutResult();
//        }
        List<PostmanTroubleOption> list = PostmanTroubleOptionMemory.getInstance().list();
        if (ValidateUtil.isAbsList(list)) {
            return AjaxResult.successResult(list.size(), list);
        }
        return AjaxResult.successResult(0, null);
    }


    //++++++++++++++++++++++++++++++++++++骑手获取评论信息++++++++++++++++++++++++++++++
    // 骑手获取评论信息
    @RequestMapping(method = RequestMethod.POST, value = "/postman/comment/list")
    @ResponseBody
    public String get_comment_(String token, String postman, Integer type,
                               @RequestParam(value = "page", defaultValue = "1") Integer page,
                               @RequestParam(value = "size", defaultValue = "20") Integer size) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null && StringUtils.isEmpty(postman)) {
            return AjaxResult.errorResult("骑手账号不能为空");
        }
        if (ValidateUtil.isNull(postman)) {
            postman = user.getUsername();
        }
        List<OrderCommentInfoVO> commentInfoVOList = PostmanMan.listOfPostmanAndStr(postman, type);
        if (ValidateUtil.isAbsList(commentInfoVOList)) {
            JYPageUtils.PageList pageList = JYPageUtils.getList(commentInfoVOList, page, size);
            return AjaxResult.successResult(pageList.totalCount, pageList.size, pageList.list);
        }
        return AjaxResult.successResult(0, 0, null);
    }

    // 骑手申诉
    @RequestMapping(method = RequestMethod.POST, value = "/postman/comment/del/apply")
    @ResponseBody
    public String postman_comment_delApply(String token, Long id, String reason, FileUploadInfo fileUploadInfo, String lang) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (ValidateUtil.isNotID(id)) {
            return AjaxResult.errorResult("id不能为空");
        }
        OrderCommentInfo commentInfo = OrderCommentMemory.getInstance().get(id);
        if (commentInfo == null) {
            return AjaxResult.errorResult("评论信息为空");
        }
        if (commentInfo.getDelApply() != null && (commentInfo.getDelApply() == COMMENT_APPLY.APPLY || commentInfo.getDelApply() == COMMENT_APPLY.PASS)) {
            return AjaxResult.errorResult("申请中或已通过");
        }
        if (ValidateUtil.isNull(reason)) {
            reason = "";
        }
        StringBuffer buffer = new StringBuffer("");
        if (fileUploadInfo != null) {
            if (null != fileUploadInfo.getImage1()) {
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.ORDER_IMAGE, fileUploadInfo.getImage1());
                if (null == filePath) {
                    return MultilingualResult.errorResultV2(PROMPTS.FAILED_SAVE_IMG, lang);
                }
                if (ValidateUtil.isNotNull(filePath)) {
                    buffer.append(" , ").append("applyImage1Url='").append(filePath).append("' ");
                }
            }
            if (null != fileUploadInfo.getImage2()) {
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.ORDER_IMAGE, fileUploadInfo.getImage2());
                if (null == filePath) {
                    return MultilingualResult.errorResultV2(PROMPTS.FAILED_SAVE_IMG, lang);
                }
                if (ValidateUtil.isNotNull(filePath)) {
                    buffer.append(" , ").append("applyImage2Url='").append(filePath).append("' ");
                }
            }
            if (null != fileUploadInfo.getImage3()) {
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.ORDER_IMAGE, fileUploadInfo.getImage3());
                if (null == filePath) {
                    return MultilingualResult.errorResultV2(PROMPTS.FAILED_SAVE_IMG, lang);
                }
                if (ValidateUtil.isNotNull(filePath)) {
                    buffer.append(" , ").append("applyImage3Url='").append(filePath).append("' ");
                }
            }
            if (null != fileUploadInfo.getImage4()) {
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.ORDER_IMAGE, fileUploadInfo.getImage4());
                if (null == filePath) {
                    return MultilingualResult.errorResultV2(PROMPTS.FAILED_SAVE_IMG, lang);
                }
                if (ValidateUtil.isNotNull(filePath)) {
                    buffer.append(" , ").append("applyImage1Url='").append(filePath).append("' ");
                }
            }
        }

        StringBuffer sql = new StringBuffer();
        sql.append(" update ").append(PojoTables.ORDER_COMMENT).append(" set delApply=").append(COMMENT_APPLY.APPLY)
                .append(buffer.toString()) // 申诉图
                .append(",").append(" applyReason='").append(reason).append("' where id =").append(id);
        try {
            UserDaoSql.getInstance().executeBySql(sql.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("申诉失败，请稍后重试");
        }

        return AjaxResult.successResult();
    }

}
